package menu;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import menu.ConfigurationMenu.MenuSelectionStatus;
import menu.MenuAction.MenuActionType;

import core.GameOptions;
import core.Player;
import core.PlayerData;
import core.V2Spinner;
import core.actions.*;

public class PlayerMenu {
	
	Point offset;
	int width=0;
	int height=0;
	private String headerText="Lulz";
	private ActionMap<MenuAction> menuMap=new ActionMap<MenuAction>();
	
	private int frame;
	private int menuSpeed = 45;  //lower is faster
	private int currentOption, currentDepth;
	
	private boolean multiClickSupport=true;
	private int dcTicker;
	private int dcFrames = 35;
	private int dcCount;
	
	
	private PlayerData playerData;
	
	private V2Spinner previewSpinner = null;
	
	public PlayerMenu(PlayerData data){
		playerData=data;
		offset=new Point(0, 0);
		width=300;
		height=00;
		regenerateSpinnerPreview();
		/*for(int i=0; i<10; i++){
			menuMap.add(new MenuAction());
		}*/
		MenuController.setMainMenu(this);
	}
	
	public void tick(){
	
		if(dcTicker>0){
			dcTicker--;
			if(dcTicker==0){
				currentDepth=dcCount-1;
				activate();
			}
		}		
		else{
			frame++;
			if(frame%menuSpeed == 0){
				currentOption++;
				if(currentOption >= menuMap.getRadius()){
					currentOption=0;
				}
			}
		}
		
	}
	
	public void click(){
		
		if(GameOptions.allowConfig){
			dcCount++;		
			if(multiClickSupport){
				currentDepth=dcCount-1;
				dcTicker=dcFrames;
			}
			else{
				//activate();
			}
		}
		else{
			new MenuAction("Color", MenuActionType.CHANGE_COLOR).activate(this);
		}
		
			
	}
	
	public void activate(){
		/*
		playerData.getActionMap().add(new FireMortar());
		regenerateSpinnerPreview();
		*/

		menuMap.getAction(currentOption, currentDepth).activate(this);
		
		currentOption=0;
		currentDepth=0;
		dcCount=0;
		dcTicker=0;
		previewSpinner.refreshGraphics();
	}
	
	
	
	public void paint(Graphics g){
		int x = offset.x;
		int y = offset.y;
		g.setColor(playerData.getPlayerColor());
		g.fillRect(x, y, width, height);
		
		if(GameOptions.allowConfig){
			g.setColor(Color.black);
			g.drawRect(x, y, width, height);
			String key = ""+playerData.getKey();
			g.drawString(""+key.toUpperCase(), x+5, y+16);
			
			if(previewSpinner != null){
				previewSpinner.paint(null, 0, g);
			}
			g.setColor(Color.black);
			g.drawString(headerText, x+40, y+16);
			for(int row=0; row <menuMap.getRadius(); row++){
				int col = 0;
				for(MenuAction ma : menuMap.getActions(row)){
					MenuSelectionStatus status = MenuSelectionStatus.DESELECTED;
					if(row==currentOption){
						if(col==currentDepth)
							status = MenuSelectionStatus.SELECTED;
						else
							status = MenuSelectionStatus.ROW;
					}
					ma.paint(new Rectangle(x+80*col, y+20+20*row, 80, 20), status, g);
					col++;
				}
			}
			
			g.setColor(Color.black);
			g.fillRect(x+width-20, y, 20, dcTicker);
		
		}
		else{
					
			g.setColor(Color.white);
			g.setFont(new Font("adf", Font.PLAIN, 100));
			FontMetrics fm = g.getFontMetrics();
			String key = ""+playerData.getKey();
			g.drawString(""+key.toUpperCase(), x+width/2-fm.charWidth(key.toUpperCase().charAt(0))/2, y+height/2+50);
			
		}
			
		
	}
	
	public char getKey(){
		return playerData.getKey();
	}
	public void setOffset(Point offset){
		this.offset=offset;
		previewSpinner.setPosition(new Point(offset.x+100, offset.y+200));
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	public void regenerateSpinnerPreview(){
		previewSpinner=new V2Spinner(playerData.getActionMap(), playerData.getPlayerColor());
		previewSpinner.setPosition(new Point(offset.x+100, offset.y+200));
		
	}

	public Point getOffset() {
		return offset;
	}

	public PlayerData getPlayerData() {
		return playerData;
	}

	public ActionMap<MenuAction> getMenuMap() {
		return menuMap;
	}

	public void setMenuMap(ActionMap<MenuAction> menuMap) {
		this.menuMap = menuMap;
	}

	public void setHeaderText(String headerText) {
		this.headerText = headerText;
	}

	public String getHeaderText() {
		return headerText;
	}

	public int getCurrentOption() {
		return currentOption;
	}

	public void setCurrentOption(int currentOption) {
		this.currentOption = currentOption;
	}

	public int getCurrentDepth() {
		return currentDepth;
	}

	public void setCurrentDepth(int currentDepth) {
		this.currentDepth = currentDepth;
	}
	
	
}
